package com.diorsunion.dbtest.db.impl;

import com.diorsunion.dbtest.db.SqlBuilder;
import com.diorsunion.dbtest.enums.ColumnType;
import com.diorsunion.dbtest.util.ColumnObject;
import org.apache.ibatis.type.*;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * The Class HSQLDBBuilder.
 *
 * @author harley-dog
 */
public class HSQLSqlBuilder implements SqlBuilder {

    @NotNull
    public String getValue(ColumnObject columnObject, int index) {
        //函数
        if (columnObject.value != null
                && columnObject.value.startsWith("FUNC{")
                && columnObject.value.endsWith("}")) {
            return (columnObject.value.substring(5, columnObject.value.length() - 1));
        }
        //自定义数据
        if (columnObject.customs != null && columnObject.customs.length > 0 && index < columnObject.customs.length) {
            String value = columnObject.customs[index];
            switch (columnObject.valueType) {
                case SYSDATE:
                    return value == null ? "now()" : (value.length()==10?"'" + value + " 00:00:00'":"'" + value + "'");
                case SYSTIME:
                    return value == null ? "now()" : "'" + value + "'";
                case TIMESTAMP:
                    return value == null ? "now()" : (value.length()==10?"'" + value + " 00:00:00'":"'" + value + "'");
                case VARCHAR:
                    return "'" + value + "'";
                default:
                    return value;
            }
        }
        switch (columnObject.valueType) {
            case SYSDATE:
                return columnObject.value == null ? "now()" : (columnObject.value.length()==10?"'" + columnObject.value + " 00:00:00'":"'" + columnObject.value + "'");
            case SYSTIME:
                return columnObject.value == null ? "now()" : "'" + columnObject.value + "'";
            case TIMESTAMP:
                return columnObject.value == null ? "now()" : (columnObject.value.length()==10?"'" + columnObject.value + " 00:00:00'":"'" + columnObject.value + "'");
            default:
                return SqlBuilder.getDefaultValue(columnObject, index);
        }
    }


    @Override
    public String getCreateTable(String tableName, Class clazz) {
        List<ColumnObject> columnObjects = SqlBuilder.getColumnsByClass(clazz);
        final StringBuilder create_sql = new StringBuilder("create table " + tableName + "(");
        columnObjects.stream().forEach(columnObject -> {
            create_sql.append(columnObject.name);
            Class typeHanlerClazz = columnObject.typeHandler.getClass();
            if (IntegerTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("int");
            } else if (ShortTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("smallint");
            } else if (StringTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("varchar(2048)");
            } else if (CharacterTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("char(255)");
            } else if (LongTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("bigint");
            } else if (DateTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("datetime");
            } else if (DoubleTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("double");
            } else if (EnumTypeHandler.class.equals(typeHanlerClazz)) {
                create_sql.append(" ").append("varchar(2048)");
            }

            if(columnObject.id){
                create_sql.append(" PRIMARY KEY NOT NULL ");
            }
            if(columnObject.increase){
                create_sql.append(" IDENTITY ");
            }
            create_sql.append(",");
        });
        create_sql.deleteCharAt(create_sql.length() - 1);
        create_sql.append(")");
        return create_sql.toString();
    }
}